use embassy::executor::{SpawnToken, Spawner};
pub use embassy::task;
use embassy::time::{Duration, Timer};
use embassy::util::Forever;

static SPAWNER: Forever<Spawner> = Forever::new();

pub fn set_global_spawner(spawner: Spawner) {
    SPAWNER.put(spawner.clone());
}

pub fn spawn<F>(token: SpawnToken<F>) {
    let result = unsafe { SPAWNER.steal() }.spawn(token);
    if let Err(e) = result {
        log::warn!("thread err: {:?}.", e);
    }
}

pub fn sleep(time: Duration) -> Timer {
    Timer::after(time)
}

pub fn sleep_ms(ms: u64) -> Timer {
    sleep(Duration::from_millis(ms))
}
